//
//  RHDeviceElectricCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/3/19.
//

#import "RHDeviceElectricCell.h"
@interface RHDeviceElectricCell()
//负载用电量
@property (nonatomic, strong) QMUILabel *loadLab;
//光伏发电量
@property (nonatomic, strong) QMUILabel *solarLab;
//电池充电量
@property (nonatomic ,strong) QMUILabel *chgLab;
//电池放电量
@property (nonatomic ,strong) QMUILabel *dchgLab;
//电网买电量
@property (nonatomic ,strong) QMUILabel *buyLab;
//电网卖电量
@property (nonatomic ,strong) QMUILabel *sellLab;



@end
@implementation RHDeviceElectricCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    NSString *solar = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"solar"], @"--")];
    NSString *load = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"load"], @"--")];
    NSString *chg = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"chg"], @"--")];
    NSString *dchg = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"dchg"], @"--")];
    NSString *buy = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"buy"], @"--")];
    NSString *sell = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"sell"], @"--")];
    
    self.solarLab.attributedText = [NSString attributeStringWithContent:solar attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
    self.loadLab.attributedText = [NSString attributeStringWithContent:load attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
    self.chgLab.attributedText = [NSString attributeStringWithContent:chg attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
    self.dchgLab.attributedText = [NSString attributeStringWithContent:dchg attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
    self.buyLab.attributedText = [NSString attributeStringWithContent:buy attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
    self.sellLab.attributedText = [NSString attributeStringWithContent:sell attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [bgView setBackgroundColor:CFTabViewBgColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,0, 0, 0));
    }];
    
    __block UIView *lastView;
    __block CGFloat itemW = (kUI_WINDOW_W-30-10)/2;
    NSArray *titles = @[@"负载".rh_localized,@"光伏".rh_localized];
    NSArray *images = @[@"kwt_pow_load",@"kwt_pow_solar"];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [weakSelf itemViewWithIndex:idx title:titles[idx] image:images[idx]];
        [itemView rounded:8 width:1 color:kBorderColor];
        [bgView addSubview:itemView];
        if(idx == 0){
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.top.mas_offset(0);
                make.width.offset(itemW);
                make.height.offset(kScaleW(65));
            }];
        }else{
            if(idx%2==0){//换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(15);
                    make.top.equalTo(lastView.mas_bottom).offset(10);
                    make.size.equalTo(lastView);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right).offset(10);
                    make.centerY.equalTo(lastView);
                    make.size.equalTo(lastView);
                }];
            }
        }
                
        lastView = itemView;
    }];
    
    //电池 充放电量
    self.chgLab = [[QMUILabel alloc]init];
    self.dchgLab = [[QMUILabel alloc]init];
    self.chgLab.text = @"--";
    self.dchgLab.text = @"--";
    
    UIView *monthView = [self itemWithTypeTitle:@"电池".rh_localized subTitle1:@"充电量".rh_localized lab1:self.chgLab subTitle2:@"放电量".rh_localized lab2:self.dchgLab icon:@"kwt_pow_battery"];
    [monthView rounded:8 width:1 color:kBorderColor];
    [bgView addSubview:monthView];
    [monthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(lastView.mas_bottom).offset(15);
    }];
    
    //电网 买卖电量
    self.buyLab = [[QMUILabel alloc]init];
    self.sellLab = [[QMUILabel alloc]init];
    self.buyLab.text = @"--";
    self.sellLab.text = @"--";
    UIView *totalView = [self itemWithTypeTitle:@"电网".rh_localized subTitle1:@"买电量".rh_localized lab1:self.buyLab subTitle2:@"卖电量".rh_localized lab2:self.sellLab icon:@"kwt_pow_grid"];
    [totalView rounded:8 width:1 color:kBorderColor];
    [bgView addSubview:totalView];
    [totalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(monthView);
        make.right.equalTo(monthView);
        make.top.equalTo(monthView.mas_bottom).offset(15);
        make.bottom.mas_offset(0);
    }];
}

-(UIView*)itemViewWithIndex:(NSInteger)idx title:(NSString*)title image:(NSString*)imageStr
{
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = UIColor.whiteColor;
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageStr]];
    [bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.numberOfLines = 2;
    titleLab.adjustsFontSizeToFitWidth = YES;
    titleLab.minimumScaleFactor = 0.6;
    titleLab.text = [NSString stringWithFormat:@"%@",title];
    titleLab.textColor = [UIColor colorWithHexString:@"#474B53" alpha:1];
    titleLab.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:titleLab];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = NSTextAlignmentLeft;
    contentLab.tag = 200 + idx;
    contentLab.text = @"--";
    contentLab.textColor = [UIColor colorWithHexString:@"#303133" alpha:1];
    contentLab.font = [UIFont boldSystemFontOfSize:16];
    [bgView addSubview:contentLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(12);
        make.right.mas_offset(-3);
        make.centerY.equalTo(img.mas_top);
    }];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(titleLab);
        make.top.equalTo(titleLab.mas_bottom).mas_offset(5);
    }];
    
   
    if (idx == 0) {
        self.loadLab = contentLab;
    }
    if (idx == 1) {
        self.solarLab = contentLab;
    }
    
    return bgView;
}


//当月数据  累计数据

-(UIView*)itemWithTypeTitle:(NSString*)title  subTitle1:(NSString *)title1 lab1:(QMUILabel*)lab1 subTitle2:(NSString*)title2 lab2:(QMUILabel*)lab2 icon:(NSString*)icon
{
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView rounded:8 width:1 color:kBorderColor];
    [self.contentView addSubview:bgView];
    
    //左上角图标
    UIImageView *iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:icon]];
    [bgView addSubview:iconImg];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    //左上角标题
    QMUILabel *titleLab = [QMUILabel labelWithText:@"--" font:kSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#303133")];
    [bgView addSubview:titleLab];
    titleLab.text = title;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(10);
        make.centerY.equalTo(iconImg);
    }];
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(iconImg.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
    
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 30;
    CGFloat itemWidth = bgWidth / 2;
    //左边第一个
    
    UIView *itemView_1 = kView;
    [itemBgView addSubview:itemView_1];
    QMUILabel *subTitleLab_1 = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
    subTitleLab_1.text = title1.rh_localized;
    [itemView_1 addSubview:subTitleLab_1];
    [itemView_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(itemWidth);
        make.left.mas_offset(0);
        make.top.mas_offset(0);
        make.height.offset(60);
        make.bottom.mas_offset(0);
    }];
    
    
    
    lab1.font = kBOLDSYSTEMFONT(16);
    lab1.textColor = kCOLOR_HEX(@"#303133");
    lab1.textAlignment = NSTextAlignmentLeft;
    [itemView_1 addSubview:lab1];
    
    [subTitleLab_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemView_1.mas_centerY).offset(3);
        make.left.mas_offset(50);
        make.right.mas_offset(-3);
    }];
    
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(50);
        make.right.mas_offset(-3);
        make.bottom.equalTo(itemView_1.mas_centerY).offset(-3);
    }];
    
    //线
    UIView *line = kView;
    line.hidden = YES;
    line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
    [itemView_1 addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(1);
        make.height.offset(35);
        make.centerY.mas_offset(0);
        make.right.equalTo(itemView_1);
    }];
    
    //右边第二个
    UIView *itemView_2 = kView;
    [itemBgView addSubview:itemView_2];
    QMUILabel *subTitleLab_2 = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
    subTitleLab_2.text = title2.rh_localized;
    [itemView_2 addSubview:subTitleLab_2];
    [itemView_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(itemWidth);
        make.left.equalTo(itemView_1.mas_right);
        make.top.mas_offset(0);
        make.height.offset(60);
    }];
    
   
    lab2.font = kBOLDSYSTEMFONT(16);
    lab2.textColor = kCOLOR_HEX(@"#303133");
    lab2.textAlignment = NSTextAlignmentLeft;
    [itemView_2 addSubview:lab2];
    
    [subTitleLab_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemView_2.mas_centerY).offset(3);
        make.left.mas_offset(50);
        make.right.mas_offset(-3);
    }];
    
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(50);
        make.right.mas_offset(-3);
        make.bottom.equalTo(itemView_2.mas_centerY).offset(-3);
    }];
    
    return bgView;
}


@end
